package com.ibix;

import com.ibix.constant.RedisKeyName;
import com.ibix.netty.communion.MyClient;
import com.ibix.print.PrintJobPack;
import com.ibix.tools.RedisTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.print.PrintService;
import java.awt.print.PrinterJob;

/**
 * Created by Administrator on 2018/2/1.
 */
@Component
public class ApplicationStartup implements CommandLineRunner {

    @Autowired
    RedisTool redisTool;

    @Override
    public void run(String... strings) throws Exception {
        RedisTool.setRedisTool(redisTool);
        String callFood = redisTool.getValue(RedisKeyName.CALL_FOOD);
        String cashier = redisTool.getValue(RedisKeyName.CASHIER);
        String cook  = redisTool.getValue(RedisKeyName.COOK);
        for(PrintService printService: PrinterJob.lookupPrintServices())
        {
            if(printService.getName().equals(callFood))
            {
                PrintJobPack.getCallFoodPrint().setPrintService(printService);
            }
            if(printService.getName().equals(cashier))
            {
                PrintJobPack.getCashierPrint().setPrintService(printService);
            }
            if(printService.getName().equals(cook))
            {
                PrintJobPack.getCookPrint().setPrintService(printService);
            }
        }
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                MyClient myClient = new MyClient();
                try {
                    myClient.start();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        Thread thread = new Thread(runnable,"thread-communion-main");
        thread.start();
    }
}
